package edu.kpi.pzks2.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import edu.kpi.pzks2.tools.GenerationParams;
import edu.kpi.pzks2.ui.task.TaskFrame;

/**
 * Generation dialog.
 *
 * @author Pustovit Michael, pustovitm@gmail.com
 */
@SuppressWarnings("serial")
public class GeneratorDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField minNodeWeight;
    private JTextField maxNodeWeight;
    private JTextField nodeCount;
    private JTextField connectivity;
    private JTextField minEdgeWeight;
    private JTextField maxEdgeWeight;

    private final TaskFrame parentFrame;

    /**
     * Create the dialog.
     */
    public GeneratorDialog(final TaskFrame taskFrame) {
        setSize(200, 270);
        setTitle("Task graph generation");
        setResizable(false);

        this.parentFrame = taskFrame;

        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new MigLayout("", "[][]", "[27.00][][14.00][32.00][32.00][][][]"));

        contentPanel.add(new JLabel("Node count"), "cell 0 0");

        nodeCount = new JTextField();
        nodeCount.setText("6");
        contentPanel.add(nodeCount, "cell 1 0,growx");
        nodeCount.setColumns(10);

        contentPanel.add(new JLabel("Node weight"), "cell 0 1");
        contentPanel.add(new JLabel("min"), "flowx,cell 0 2,alignx right");

        minNodeWeight = new JTextField();
        minNodeWeight.setText("1");
        contentPanel.add(minNodeWeight, "cell 1 2,growx");

        JLabel lblMax = new JLabel("max");
        contentPanel.add(lblMax, "cell 0 3,alignx right");

        maxNodeWeight = new JTextField();
        maxNodeWeight.setText("10");
        contentPanel.add(maxNodeWeight, "cell 1 3");
        maxNodeWeight.setColumns(10);

        JLabel connectivityLabel = new JLabel("Connectivity");
        contentPanel.add(connectivityLabel, "cell 0 4");

        connectivity = new JTextField();
        connectivity.setText("0.5");
        contentPanel.add(connectivity, "cell 1 4,growx");
        connectivity.setColumns(10);

        JLabel lblEdgeWeight = new JLabel("Edge weight");
        contentPanel.add(lblEdgeWeight, "cell 0 5");

        JLabel lblMinEdgeWeight = new JLabel("min");
        contentPanel.add(lblMinEdgeWeight, "cell 0 6,alignx right");

        minEdgeWeight = new JTextField();
        minEdgeWeight.setText("1");
        contentPanel.add(minEdgeWeight, "cell 1 6,growx");
        minEdgeWeight.setColumns(10);

        contentPanel.add(new JLabel("max"), "cell 0 7,alignx right");
        maxEdgeWeight = new JTextField();
        maxEdgeWeight.setText("20");
        contentPanel.add(maxEdgeWeight, "cell 1 7,growx");
        maxEdgeWeight.setColumns(10);
        
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);

        JButton okButton = new JButton("Generate");
        buttonPane.add(okButton);
        getRootPane().setDefaultButton(okButton);

        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final int minNodeWeight = Integer.parseInt(GeneratorDialog.this.minNodeWeight.getText());
                final int maxNodeWeight = Integer.parseInt(GeneratorDialog.this.maxNodeWeight.getText());
                final int minEdgeWeight = Integer.parseInt(GeneratorDialog.this.minEdgeWeight.getText());
                final int maxEdgeWeight = Integer.parseInt(GeneratorDialog.this.maxEdgeWeight.getText());
                final float connectivity = Float.parseFloat(GeneratorDialog.this.connectivity.getText());
                final int nodeCount  = Integer.parseInt(GeneratorDialog.this.nodeCount.getText());
                
                GeneratorDialog.this.parentFrame.generateGraph(
                        new GenerationParams(
                                minNodeWeight, 
                                maxNodeWeight, 
                                minEdgeWeight, 
                                maxEdgeWeight,
                                connectivity, 
                                nodeCount));
                GeneratorDialog.this.dispose();
            }
        });

        JButton cancelButton = new JButton("Cancel");
        buttonPane.add(cancelButton);
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GeneratorDialog.this.dispose();
            }
        });
    }

}
